package com.cwt_taxi_xa;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.data.DataUtil;
import com.cwt_taxi_xa.protocol.MeterEncoder;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.DateUtil;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.TTsFunction;

import java.util.Timer;
import java.util.TimerTask;

public class ActivityQT extends CWTBaseActivity{

	TextView tv_qiantui_cancel, tv_qiantui_tip;

	boolean shuaka = false;
	boolean jjqqt = false;

	long yanzhengTime = 0;

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what == 1001){
				jjqqttime = 0;
				String info = (String) msg.obj;
				Log.e("ActivityQT-Handler-1001", info);
				SPUtils.setParam(SPConstants.SPK_signin, false);
				TTsFunction.speakText("签退成功");
				Intent intent = new Intent(ActivityQT.this, ActivityEnd.class);
				intent.putExtra("info", info);
				startActivity(intent);//---------------------------------------------------------------------------
				finish();
			}
			else if(msg.what==1002){
				TTsFunction.speakText("请刷签到司机卡");
				finish();
			}
			else if(msg.what==1003){
				if(System.currentTimeMillis()-jjqqttime>50*1000){
					finish();
				}else{
					tv_qiantui_tip.setText(Html.fromHtml("计价器申请签退<br>确认请刷<b><font color=#a6ff6f>" + " IC " + "</b>卡<br>误操作请等待<br>计价器恢复倒计时：<b><font color=#a6ff6f>" + "   "+(50-((System.currentTimeMillis()-jjqqttime)/1000))+"s " + "</b>"));


//					Driver driver = DataUtil.getNowDriverInfor();
//					SerialPortService.getInstance().sendSerialData6(MeterEncoder.sendDriverInfoForPower905("00E3",
//							"12345",
//							"12345",
//							SPUtils.getParamString(SPConstants.SPK_carcode, SPConstants.SPV_carcode),
//							DateUtil.getNowDateToMeter(), 1, "0000000000",
//							"0000", "90"));
				}
			}
			else if(msg.what==1006){

				if(yanzhengTime!=0&&System.currentTimeMillis()-yanzhengTime>1000){
					yanzhengTime = 0;

					qiantui();
				}

				if(Constants.sijiNow){
					qiantui();
				}else{
					tv_qiantui_tip.setText("验证卡信息，请稍等");
					yanzhengTime = System.currentTimeMillis();
					handler.sendEmptyMessageDelayed(1006, 1500);
				}

			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_qt);

		tv_qiantui_cancel = (TextView) findViewById(R.id.tv_qiantui_click1);
		tv_qiantui_tip = (TextView) findViewById(R.id.tv_qiantui_tip);

		tv_qiantui_cancel.setOnClickListener(onClick);
	}


	protected void qiantui() {
		String signSJcode = SPUtils.getParamString(SPConstants.SPK_signinNum, SPConstants.SPV_signinNum);
		String sijicode = DataUtil.getDriverCode();




		if(signSJcode==null||signSJcode.equals("")||signSJcode.equals(DataUtil.sijicode)||!Constants.sijiNow){
			AppLog.e("-----------异常-----------强制发送计价器签退信息");
			sendqiantui();
			return;
		}

		if(signSJcode.equals(sijicode)){
			AppLog.e("-----------正常-----------发送计价器签退信息");
			sendqiantui();
		}else{
			handler.sendEmptyMessage(1002);
			AppLog.e("司机从业资格证号对比---卡数据："+sijicode+"    本地数据："+signSJcode);
		}


	}

	void sendqiantui(){
		shuaka = true;
		steps();
		if(DataUtil.getJJQ()==1){
			tv_qiantui_tip.setText(Html.fromHtml("刷卡签退<br>确认请按IC键<b><font color=#a6ff6f>" + " 2 " + "</b>秒<br>误操作请点击取消"));
		}else{
			tv_qiantui_tip.setText(Html.fromHtml("刷卡签退<br>确认请按单程键<b><font color=#a6ff6f>" + " 3 " + "</b>秒<br>误操作请点击取消"));
		}
	}


	public void steps(){
		if(shuaka&&jjqqt){
			Log.e("MeterBusinessProcess", "发送司机信息关机");
			SerialPortService.getInstance().sendSerialData6(MeterEncoder.sendDriverInfoForPower905("00E3",
					DataUtil.getCompanyCode(),
					DataUtil.getDriverCode(),
					SPUtils.getParamString(SPConstants.SPK_carcode, SPConstants.SPV_carcode),
					DateUtil.getNowDateToMeter(), 1, "0000000000",
					"0000", "90"));
		}
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
				case R.id.tv_qiantui_click1:
					jjqqt = false;
					shuaka = false;
					finish();
					break;
			}
		}
	};

	private static long jjqqttime;
	Timer jjqtimer;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Intent intent = getIntent();
		//1-计价器发起   2-刷卡器发起
		int type = intent.getIntExtra("type", 0);
		if(type==1){
			if(System.currentTimeMillis()-jjqqttime>50*1000){
				jjqqttime = System.currentTimeMillis();
				TTsFunction.speakText("计价器申请签退");
			}else{
				TTsFunction.speakText("请等待计价器恢复");
			}

			jjqqt = true;


			jjqtimer = new Timer();
			jjqtimer.schedule(new TimerTask() {
				@Override
				public void run() {
					handler.sendEmptyMessage(1003);
				}
			}, 0, 1000);

		}

		if(type==2){
			handler.sendEmptyMessage(1006);
			TTsFunction.speakText("是否刷卡签退？");
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(jjqtimer!=null){
			jjqtimer.cancel();
		}
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		if(type == 1005){
			jjqqt = true;
			steps();
		}
		if(type == 1006){
			AppLog.e("-------收到签退成功信息");
			handler.sendMessage(handler.obtainMessage(1001, obj));
		}
		if(type == 1007){
			handler.sendEmptyMessage(1006);
		}
	}

}
